package com.net.xpay.common.manager.impl;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.net.xpay.common.manager.UserManager;
import com.net.common.annotation.ReadTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractVersionedManager;
import com.net.xpay.common.dao.UserMapper;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.enums.user.CommissionMode;
import com.net.xpay.common.model.search.UserSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Component
@ReadTransactional
public class UserManagerImpl extends AbstractVersionedManager<User> implements UserManager {

    @Autowired
    private UserMapper mapper;


    @Override
    protected BasicMapper<Long, User> getMapper() {
        return mapper;
    }

    @Override
    public User findByMobile(String mobile) {
        Preconditions.checkArgument(Objects.nonNull(mobile));

        User condition = new User();
        condition.setMobile(mobile);

        return getByCondition(condition);
    }

    @Override
    public List<User> search(UserSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");

        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public List<Long> listIds(UserSearchModel searchModel) {
        List<Long> ids = mapper.listIds(searchModel);
        return ids == null ? new ArrayList<>() : ids;
    }

    @Override
    public long searchCount(UserSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }

    @Override
    public User findByAppkey(String appkey) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(appkey));
        User condition = new User();
        condition.setAppkey(appkey);
        return getByCondition(condition);
    }

    @Override
    public List<User> listByPartnerId(Long partnerId) {
        Preconditions.checkArgument(partnerId != null);
        User condition = new User();
        condition.setPartnerId(partnerId);
        return listByCondition(condition, Lists.newArrayList());
    }

    @Override
    public List<User> listAll() {
      return listByCondition(new User(),Lists.newArrayList());
    }

    @Override
    public User findByChannelMasterIdAndOuterId(Long channelMasterId, String outerId) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(outerId));
        User condition = new User();
        condition.setChannelMasterUserId(channelMasterId);
        condition.setOuterId(outerId);
        return getByCondition(condition);
    }

    @Override
    public User findByUid(String uid) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(uid));
        User condition = new User();
        condition.setUid(uid);
        return getByCondition(condition);
    }

    @Override
    public long countRegister(Long partnerId, LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument(partnerId != null && minEndTime != null && maxEndTime != null);
        return mapper.countRegister(partnerId, minEndTime, maxEndTime);
    }

    @Override
    public long countUserRegister(LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        return mapper.countUserRegister(minEndTime, maxEndTime);
    }

    @Override
    public List<User> listByCommissionMode(CommissionMode mode) {
        Preconditions.checkArgument(mode != null );
        List<User> users = mapper.listByCommissionMode(mode);
        return users == null ? new ArrayList<>() : users;
    }

    @Override
    public User findByName(String name) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
        User condition = new User();
        condition.setName(name);
        return getByCondition(condition);
    }


}
